package com.hunliji.monitor.web.controller.app;

import com.alibaba.fastjson.JSON;
import com.hunliji.common.response.ResponseData;
import com.hunliji.monitor.web.facade.AliyunNotifyFacade;
import com.hunliji.monitor.web.facade.FeiShuFacade;
import com.hunliji.monitor.web.facade.InformFacade;
import com.hunliji.monitor.web.model.param.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用来接收通用事件
 * @author wuchen
 */
@Slf4j
@RequestMapping(value = "/appApi/notify")
@RestController
public class NotifyAppController {
    @Resource
    private FeiShuFacade feiShuFacade;

    @Resource
    private AliyunNotifyFacade aliyunNotifyFacade;

    @Resource
    private InformFacade informFacade;

    /**
     * 事件测试
     * @return
     * @throws Exception
     */
    @PostMapping("/test")
    public ResponseData test(@RequestBody List<NotifyParam> param){
        log.info("test-测试事件 消息推送，参数打印:{}", JSON.toJSONString(param));
        if(CollectionUtils.isEmpty(param)){
            log.warn("通知数据异常,无数据");
            return ResponseData.success(null);
        }
        NotifyParam notifyParam = param.get(0);
        log.info("test-测试事件,开始处理 消息推送，参数打印:{}", JSON.toJSONString(notifyParam));
        feiShuFacade.handleNotify(notifyParam.getReceiveSign(),notifyParam.getContent());
        return ResponseData.success(null);
    }

    /**
     * 触发告警
     * @param param
     * @return
     */
    @PostMapping("/alert")
    public ResponseData alert(@RequestBody List<AliyunAlertNotifyParam> param){
        aliyunNotifyFacade.alert(param);
        return ResponseData.success(Boolean.TRUE);
    }

    /**
     * 触发告警
     * @param informParams
     * @return
     */
    @PostMapping("/inform")
    public ResponseData inform(@RequestBody List<InformParam> informParams){
        for (InformParam informParam:informParams){
            informFacade.inform(informParam);
        }
        return ResponseData.success(Boolean.TRUE);
    }

    /**
     * 通知添加群绑定
     * @param addOrUpdateInformChatGroupParam
     */
    @PostMapping("/updateInformChatGroup")
    public ResponseData updateInformChatGroup(@RequestBody AddOrUpdateInformChatGroupParam addOrUpdateInformChatGroupParam) {
        informFacade.updateInformChatGroup(addOrUpdateInformChatGroupParam);
        return ResponseData.success(null);
    }

    /**
     * 添加at
     * @param addOrUpdateInformAtParam
     */
    @PostMapping("/addInformAt")
    public ResponseData addInformAt(@RequestBody AddOrUpdateInformAtParam addOrUpdateInformAtParam){
        informFacade.addInformAt(addOrUpdateInformAtParam);
        return ResponseData.success(null);
    }

    /**
     * 更新at
     * @param updateInformAtParam
     * @return
     */
    @PostMapping("/updateInformAt")
    public ResponseData updateInformAt(@RequestBody UpdateInformAtParam updateInformAtParam){
        informFacade.updateInformAt(updateInformAtParam);
        return ResponseData.success(null);
    }

    /**
     * 删除at
     * @param updateInformAtParam
     * @return
     */
    @PostMapping("/delInformAt")
    public ResponseData delInformAt(@RequestBody UpdateInformAtParam updateInformAtParam){
        informFacade.delInformAt(updateInformAtParam);
        return ResponseData.success(null);
    }





}
